//
//  MineZuanViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/6.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//我的-我的钻石

#import "MineZuanViewController.h"
#import "MineZuanTableViewCell.h"
#import "MineZuanOperator.h"
#import "MineZuanModel.h"
#import "MineZuanDataModel.h"
#import "MineZuanListModel.h"
#import "MineZuanExViewController.h"
#import "MineChongzuanViewController.h"

@interface MineZuanViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArray;

@property (weak, nonatomic) IBOutlet UILabel *zuanCountLabel;

@end

@implementation MineZuanViewController

static NSString *zuanCellIdentifier = @"MineZuanTableViewCell";

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

- (void)setupNavigation
{
    self.navigationItem.title = @"我的钻石";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"说明" style:UIBarButtonItemStylePlain target:self action:@selector(clickRight)];
}

- (void)setupView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigation];
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineZuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zuanCellIdentifier];
    if (!cell) {
        cell = [[UINib nibWithNibName:@"MineZuanTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    MineZuanListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MineChongzuanViewController *chong = [[MineChongzuanViewController alloc] init];
    MineZuanListModel *model = self.dataArray[indexPath.row];
    chong.model = model;
    [self.navigationController pushViewController:chong animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)clickRight
{
    MineZuanExViewController *zuan = [[MineZuanExViewController alloc] init];
    [self.navigationController pushViewController:zuan animated:YES];
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    MineZuanOperator *operator = [[MineZuanOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = operator.data.data.list;
            self.zuanCountLabel.text = operator.data.data.my;
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
